package org.javabb.dao.hibernate;

import java.util.List;

import org.javabb.dao.entity.ICategoryDAO;
import org.javabb.vh.Stats;
import org.javabb.vo.Category;
import org.javabb.vo.Post;
import org.javabb.vo.Topic;
import org.javabb.vo.User;

/*
 * Copyright 2004 JavaFree.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * $Id: CategoryHibernateDAO.java,v 1.9.8.4 2007/08/31 21:04:43 daltoncamargo Exp $
 * @author Dalton Camargo - <a href="mailto:dalton@javabb.org">dalton@javabb.org </a> <br>
 * @author Ronald Tetsuo Miura
 */
public class CategoryHibernateDAO extends HibernateDAO implements ICategoryDAO {

    static final String[] FIELD_NAMES = new String[] { null,
        "idCategory",
        "catOrder",
        "nameCategory" };

    /**
     * @see org.javabb.dao.entity.ICategoryDAO#load(java.lang.Long)
     */
    public Category load(Long id) {
        return (Category) getHibernateTemplate().load(Category.class, id);
    }

    /**
     * @see org.javabb.dao.entity.ICategoryDAO#countAllPosts()
     */
    public int countAllPosts() {
        return countRows(Post.class, "idPost");
    }

    /**
     * @see org.javabb.dao.entity.ICategoryDAO#countAllTopics()
     */
    public int countAllTopics() {
        return countRows(Topic.class, "idTopic");
    }

    /**
     * @see org.javabb.dao.entity.ICategoryDAO#countAllUsers()
     */
    public int countAllUsers() {
        return countRows(User.class, "idUser");
    }

    /**
     * @see org.javabb.dao.hibernate.HibernateDAO#getFieldName(int)
     */
    protected String getFieldName(int i) {
        return FIELD_NAMES[i];
    }

    /**
     * @see org.javabb.dao.entity.ICategoryDAO#getStatistics()
     */
    public Stats getStatistics() {
        String[] orderField = { "idUser" };
        String[] orderValue = { "desc" };

        User lastUser = null;
        List lstUsers = findAll(User.class, orderField, orderValue, 0, 1);

        if (!lstUsers.isEmpty()) {
            lastUser = (User) lstUsers.get(0);
        }
        lstUsers = null;

        Stats sts = new Stats();
        sts.setLastRegisteredUserId(lastUser.getIdUser());
        sts.setLastRegisteredUserName(lastUser.getUser());
        return sts;
    }

    /**
     * @param sortingFields
     * @return
     */
    public List findAll(int[] sortingFields) {
        return findAll(Category.class, sortingFields);
    }
    
    /**
     * Insert a category
     * @param cat
     * @return
     */
    public Category insertCategory(Category cat){
    	return (Category)getHibernateTemplate().saveOrUpdateCopy(cat);
    }

    public Category getLastCattegoryByOrder(){
        Category cat = null;
        List lstCats = findAll(Category.class,
        		new String[]{"catOrder"},
				new String[]{"desc"},
				0, 1);
        
        if (!lstCats.isEmpty()) {
        	cat = (Category) lstCats.get(0);
        }
        return cat;
    }
    
    /**
     * Delete category
     * @param cat
     */
    public void deleteCategory(Category cat){
    	this.getHibernateTemplate().delete(cat);
    }
}
